package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     *
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result page(String name, Integer gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("员工列表查询:{},{},{},{},{},{}", name, gender, begin, end, page, pageSize);
        PageBean pageQuery = empService.pageQuery(name, gender, begin, end, page, pageSize);
        return Result.success(pageQuery);

    }
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Integer id){
        Emp emp=empService.queryId(id);
        return Result.success(emp);
    }

    //更新操作
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        empService.modifyEmp(emp);
        return Result.success();
    }

    //新增操作
    @PostMapping
    public Result insert(@RequestBody Emp emp) {
        empService.addEmp(emp);
        return Result.success();
    }


    @DeleteMapping("/{ids}")
    public Result batchRemove(@PathVariable List<Integer> ids) {
        empService.batchRemoveEmp(ids);
        return Result.success();
    }
}
